package cn.imu.appdevexperiment2022.two;

import androidx.annotation.Nullable;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import cn.imu.appdevexperiment2022.R;
import cn.imu.appdevexperiment2022.start.BaseActivity;

public class FirstActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        // 提示信息Toast
        Button button_first_toast = (Button)findViewById(R.id.Button_first_toast);
        button_first_toast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(FirstActivity.this,"你点击了按钮Button_first_toast",
                        Toast.LENGTH_SHORT).show();
            }
        });
        // 销毁活动，就是返回finish
        Button button_first_finish = (Button)findViewById(R.id.Button_first_finish);
        button_first_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //显示调用Intent：跳转活动
        Button button_first_intent1 = (Button)findViewById(R.id.Button_first_intent1);
        button_first_intent1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });
        // 隐示调用Intent:跳转活动
        Button button_first_intent2 = (Button)findViewById(R.id.Button_first_intent2);
        button_first_intent2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("cn.imu.appdevexperiment2020.ACTION_START");
                startActivity(intent);
            }
        });
        // 隐示调用Intent：跳转网页
        Button button_first_intent3 = (Button)findViewById(R.id.Button_first_intent3);
        button_first_intent3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("http://www.baidu.com"));
                startActivity(intent);
            }
        });
        // 向下一个活动传递数据
        Button button_first_intent4 = (Button)findViewById(R.id.Button_first_intent4);
        button_first_intent4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String data = "你好 SecondaActivity";
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                intent.putExtra("extra_data",data);
                startActivity(intent);
            }
        });
        // 返回数据给上一个活动
        Button button_first_intent5 = (Button)findViewById(R.id.Button_first_intent5);
        button_first_intent5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    // 创建menu
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_item:
                Toast.makeText(this, "你点击了添加", Toast.LENGTH_LONG).show();
                break;
            case R.id.remove_item:
                Toast.makeText(this, "你点击了删除", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
        }
        return true;
    }

    // 接收下级活动的数据
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String returnData = data.getStringExtra("data_return");
                    Log.d("你好FirstActivity", returnData);
                }else {
                    Log.d("你好FirstActivity", "cc"+requestCode);
                }
                break;
            default:
                break;
        }
    }
}